﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
using System.Data;
using TireBias.Models;
using WinNemoLib;
using CrystalDecisions.Shared;

namespace TireBias.Controllers
{
    public class DemoReportController : Controller
    {
        //
        // GET: /DemoReport/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Report(string code = ""
            , string dateExport = "", string shift = ""
            , string topcode = "", string toplot = ""
            , string rep = "a1", string roll="")
        {
            try
            {
                return File(this.ExportPDF(code, dateExport, shift, topcode, toplot, rep,roll), "application/pdf");
            }
            catch (Exception ex)
            {
                WinNemoLib.Nemo.Error(ex);
                throw;
            }
           
        }

        public Stream ExportPDF(string code, string dateExport, string shift
            ,string topcode,string toplot,string rep,string roll)
        {
            // code is biasno
            try
            {
                Models.ReportModel s = new Models.ReportModel();
                ReportClass report = new ReportClass();
                ReportDocument rp = new ReportDocument();
                DataSet ds = new DataSet();
                DataTable dt = new DataTable(); 
                DataTable dt1 =new DataTable();
                DataTable dt2 = new DataTable();
                DataTable dt3 = new DataTable();
                WasteModel wMd = new WasteModel();
                LossModel lMd = new LossModel();
                switch (rep)
                {
                    case  "a1" :
                        rp.FileName = Server.MapPath(@"~\Report\Reportsum.rpt");
                        dt = s.SEL_Report(code, dateExport, shift, topcode, toplot,"");   
                        dt1= wMd.SEL_WASTE_REPORT(dateExport, shift,"","",code);
                        break;
                    case "a2":
                        rp.FileName = Server.MapPath(@"~\Report\ReportsumByRoll.rpt");
                        dt = s.SEL_Report(code,"","",topcode, toplot,roll); 
                        dateExport = DateUtil.ToDisplayDate(dt.Rows[dt.Rows.Count-1]["prd_date"], DateUtil.eLanguage.EN);
                        shift = dt.Rows[dt.Rows.Count-1]["shift"].ToString();
                       
                        dt1= wMd.SEL_WASTE_REPORT(dateExport, shift,toplot,roll,code);
                        dt3 = s.SEL_Topping(topcode, toplot, roll);
                        dt3.TableName = "RptHeadNTail";
                        ds.Tables.Add(dt3.Copy());
                        if (dt1 == null)
                        {
                            dt1 = new DataTable();
                            dt1.Columns.Add("RS_CODE", typeof(string));
                            dt1.Columns.Add("RS_DESC", typeof(string));
                            dt1.Columns.Add("qty", typeof(string));
                            dt1.Columns.Add("shift", typeof(string));
                            dt1.Columns.Add("wsdate", typeof(DateTime));
                            dt1.Rows.Add(dt1.NewRow());
                        }

                        dt2 = lMd.SEL_LOSS_DT(dateExport, shift,code,topcode,toplot,roll);
                        if (dt2 == null)
                        {
                            dt2 = new DataTable();
                            dt2.Columns.Add("LS_CODE", typeof(DateTime));
                            dt2.Columns.Add("LS_START", typeof(string));
                            dt2.Columns.Add("LS_END", typeof(string));
                            dt2.Columns.Add("LOSS", typeof(string));
                            dt2.Columns.Add("TEXT", typeof(string));
                            dt2.Columns.Add("LS_DESC", typeof(string));
                            dt2.Rows.Add(dt2.NewRow());
                        }
                       
                        dt2.TableName = "vwBIASLoss";
                        break;
                    default:
                        break;
                }
   
                rp.Load(rp.FileName);            
         
                dt1.TableName = "vwBIASWASTE";     
                
                rp.Subreports["ViewWast"].Database.Tables[0].SetDataSource(dt1);
                if (rep == "a2")
                {
                    rp.Subreports["LossData"].Database.Tables[0].SetDataSource(dt2);
                                    

                }
                dt.TableName = "report";
                ds.Tables.Add(dt.Copy());
                rp.SetDataSource(ds);
                if (rep == "a2")
                {
                    ParameterDiscreteValue param1 = new ParameterDiscreteValue();
                    ParameterDiscreteValue param2 = new ParameterDiscreteValue();
                    rp.ParameterFields[0].CurrentValues.Clear();
                    rp.ParameterFields[1].CurrentValues.Clear();
                    param1.Value = shift;
                    param2.Value = DateUtil.ToDisplayBOLDate(dateExport, DateUtil.eFormat.longDate, DateUtil.eLanguage.TH); ;
                    rp.ParameterFields[0].CurrentValues.Add(param1);
                    rp.ParameterFields[1].CurrentValues.Add(param2);   
                }
                Stream stream = rp.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                return stream;
            }
            catch (Exception ex)
            {
                WinNemoLib.Nemo.Error(ex);
                throw;
            }

        }

    }
}
